package com.odais.Reservation;

import java.util.ArrayList;

import com.odais.AE_Planner.SceneContext;
import com.odais.DataManagement.DataManager;

public class ReservationListModifierManager
{
// ===========================================================
// Constants
// ===========================================================
	static final int Offset = 2;
	static final int BOX_Height = 80;
	static final int CAMERA_WIDTH_INFO = 150;
	static final int CAMERA_HEIGHT_INFO = 380-BOX_Height/2;

// ===========================================================
// Fields
// ===========================================================
	private int mReservationListSize = 0;
	SceneContext LeftSceneContext, RightSceneContext;
	ReservationList mReservationList;
	ReservationEntityListsHandler reservationEntityListsHandler;
	private ArrayList<ReservationEntity> mReservationEntityList;
	float finalX, finalY;
	boolean mRefreshAllowed = true;
	private int validBoxCnt;

	Reservation tmpReservation;
	ReservationEntity tmpReservationEntity;
	
// ===========================================================
// Constructors
// ===========================================================
	public ReservationListModifierManager(ReservationList reservationList, ReservationEntityListsHandler reservationEntityListsHandler, SceneContext leftSceneContext, SceneContext rightSceneContext)
	{
		this.mReservationList = reservationList;
		this.reservationEntityListsHandler = reservationEntityListsHandler;
		this.mReservationEntityList = this.reservationEntityListsHandler.getReservationEntityArrayList();
		this.LeftSceneContext = leftSceneContext;
		this.RightSceneContext = rightSceneContext;
		
		mRefreshAllowed = true;
	}
	 

// ===========================================================
// Getter & Setter
// ===========================================================


// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
	public void Refresh()
	{
		//This action triggers boxes re-alignment and boxlist update
		
		//Refresh boxes lists
		reservationEntityListsHandler.Refresh();
		
		if(mReservationList.size()!=mReservationListSize)
		{
			//TODO need to kill/manage boxes separately
			for(int i=0; i<mReservationEntityList.size(); i++)
			{
				//Make it non visible
				//mReservationBoxList.get(i).setVisible(false, false);
				//Destroy in scene A
				//mReservationBoxList.get(i).killA();
				//Destroy in scene B
				//mReservationBoxList.get(i).killB();
				//Dispose the instantiation
				//mReservationBoxList.get(i).dispose();
				
				//mReservationEntityList.get(i).getReservationBox_LeftPanel().kill();
				
				//TODO check if no side effect by measuring the heap
			}
			
			mReservationListSize = mReservationList.size();

			
			mReservationList.sortByDate();
			
			//Create the list with existing reservation
			//Check status and create box if pending
			for(int i=0; i<mReservationList.size(); i++)
			{
				tmpReservation = mReservationList.get(i);
				tmpReservationEntity = new ReservationEntity(LeftSceneContext, RightSceneContext);
				tmpReservationEntity.setReservation(tmpReservation);
				
				if(tmpReservationEntity.getReservation().getItemStatus()==DataManager.RESERVATION_STATUS_PENDING)
					tmpReservationEntity.setReservationBox_LeftPanel(new ReservationBox(LeftSceneContext, 150/2, 0));
				
				mReservationEntityList.add(tmpReservationEntity);
			}
//			
//			mReservationEntityList.clear();
//			for(int i=0; i<mReservationList.size(); i++)
//			{
//				tmpReservation = mReservationList.get(i);
//				tmpReservationEntity = new ReservationEntity(LeftSceneContext);
//				tmpReservationEntity.setReservation(tmpReservation);
//				tmpReservationEntity.setReservationBox_LeftPanel(new ReservationBox(LeftSceneContext, 150/2, 0));
//				mReservationEntityList.add(tmpReservationEntity);
//			}
			
			mRefreshAllowed = true;
		}
		
		//Update list and check new arrivals
		if(mRefreshAllowed && (mReservationEntityList.size()>0))
		{
			mRefreshAllowed = false;
			//mReservationList.sortByDate();
			
			//Positioning items 
			finalX = 150/2;
			validBoxCnt = 0;
			for(int i=0; i<mReservationEntityList.size(); i++)
			{
				//Compute each element's final position
				finalY = CAMERA_HEIGHT_INFO - validBoxCnt*(BOX_Height+Offset);
				
				if(!mReservationEntityList.get(i).getReservationBox_LeftPanel().getGrabbed() & mReservationEntityList.get(i).getReservationBox_LeftPanel().getVisible())
				{
					validBoxCnt++;
				
					//Register a new modifier for each element
					//TODO skip unnecessary movement and grabbed item
					mReservationEntityList.get(i).getReservationBox_LeftPanel().animate(LeftSceneContext, mReservationEntityList.get(i).getReservationBox_LeftPanel().getX(), mReservationEntityList.get(i).getReservationBox_LeftPanel().getY(), finalX, finalY, 1, 1, 2);
					mReservationEntityList.get(i).getReservationBox_RightPanel().animate(RightSceneContext, mReservationEntityList.get(i).getReservationBox_RightPanel().getX(), mReservationEntityList.get(i).getReservationBox_RightPanel().getY(), finalX, finalY, 1, 1, 2);
				}
			}
		}
	}


	public boolean ismRefreshAllowed() {
		return mRefreshAllowed;
	}


	public void setmRefreshAllowed(boolean mRefreshAllowed) {
		this.mRefreshAllowed = mRefreshAllowed;
	}
	
	public void setReservationList(ReservationList resList)
	{
		mReservationList = resList;
	}
	

}